package com.local.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.threads.UpdateStationRunble;
import com.cloud.threads.UploadStationRunble;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.EquipmentToMedieMapper;
import com.local.dao.mapers.LocalEquipmentMapper;
import com.local.dao.mapers.LocalStationMapper;
import com.local.entity.ex.CrossingEx;
import com.local.entity.ex.LocalEquipmentEx;
import com.local.entity.ex.LocalStationEx;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.Crossing;
import com.local.model.EquipmentToMedie;
import com.local.model.EquipmentToMedieExample;
import com.local.model.LocalEquipment;
import com.local.model.LocalStation;
import com.local.model.LocalStationExample;
import com.local.page.LocalStationPage;
import com.local.service.face.CrossingService;
import com.local.service.face.LocalStationService;
import com.local.service.face.LogService;
import com.local.service.face.StreamingMediaService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.BeanUtil;
import com.qianhai.common.utils.UUIDGenerateUtil;

@Service
public class LocalStationServiceImpl extends BaseServiceImpl<LocalStationExample, LocalStation, String> implements LocalStationService
{
	@Autowired
	private LocalStationMapper localStationMapper;
	
	@Autowired
	private LocalEquipmentMapper localEquipmentMapper;
	
	@Autowired
	private StreamingMediaService streamingMediaService;
	
	@Autowired
	private CrossingService crossionService;
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private EquipmentToMedieMapper equipmentToMedieMapper;
	
	@Override
	public List<LocalStation> findAllLocalStationByQueryForm(
			LocalStationPage<LocalStation> pageInfo)
	{
		Integer recordCount = localStationMapper.countAllByPage(pageInfo);
		pageInfo.setTotalRecords(recordCount);
		List<LocalStation> list = localStationMapper.queryByPage(pageInfo);
		pageInfo.setData(list);
		return list;
	}
	
	/**
	 * 新增 岗亭
	 */
	public void addLocalStation(LocalStation localStation)
	{
		try{
			if(Tools.isEmpty(localStation.getStationName())){
				throw new BaseException("15000-00000-00022");
			}
			LocalStation staL=findByStationName(localStation.getStationName());
			if(!Tools.isEmpty(staL)){
				throw new BaseException("15000-00000-00023");
			}
			localStation.setStationUuid(UUIDGenerateUtil.generate());
			localStationMapper.insert(localStation);
			if(CloudConfigManager.instance().isOpen())
			{
				UploadStationRunble upload = new UploadStationRunble(localStation,logService);
				ThreadPoolUtil.instance().execute(upload);
			}
		}catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
	@Override
	public void modifyLocalStation(LocalStation localStation)
	{
		try
		{
			
			if (Tools.isEmpty(localStation.getStationUuid()))
			{
				throw new BaseException("50000-00001-00001");
			}
			if(Tools.isEmpty(localStation.getStationName())){
				throw new BaseException("15000-00000-00022");
			}
			if(!localStationMapper.selectByPrimaryKey(localStation.getStationUuid()).getStationName().equalsIgnoreCase(localStation.getStationName())){
				LocalStation staL=findByStationName(localStation.getStationName());
				if(!Tools.isEmpty(staL)){
					throw new BaseException("15000-00000-00023");
				}
			}
			// 判断文件流是否重复
			if (!Tools.isEmpty(localStation.getStream()))
			{
				LocalStationExample condition = new LocalStationExample();
				condition
						.or()
						.andStationUuidNotEqualTo(
								localStation.getStationUuid().trim())
						.andStreamEqualTo(localStation.getStream().trim());
				List<LocalStation> list = localStationMapper
						.selectByCondition(condition);
				if (!list.isEmpty())
				{
					throw new BaseException("50000-00001-00002");
				}
			}
			
			localStationMapper.updateByPrimaryKeySelective(localStation);
			if(CloudConfigManager.instance().isOpen())
			{
				UpdateStationRunble upload = new UpdateStationRunble(localStation,logService);
				ThreadPoolUtil.instance().execute(upload);
			}
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
	@Override
	public LocalStation findUniqueStation(String stationUuid)
	{
		LocalStation localStation = localStationMapper
				.selectByPrimaryKey(stationUuid);
		//查找出路口信息
		LocalStationEx localStationEx = new LocalStationEx();
		if(!Tools.isEmpty(localStation))
		{
		   BeanUtil.copyProperties(localStationEx, localStation);
		   List<Crossing> list = crossionService.queryCrossinBySationId(stationUuid);
		   
		   if(Tools.isEmpty(list))
		   {
			   list = new ArrayList<Crossing>();
		   }
		   //查询设备列表
		   List<Crossing> crssionExList = new ArrayList<Crossing>();
		   for(Crossing crInfo : list)
		   {
			   //是否显示视频，路口配置（路口==2 表示在岗亭显示是隐藏）
			   if(!Tools.isEmpty(crInfo.getResolution()) && crInfo.getResolution().equals("2"))
			   {
				   continue;
			   }
			   CrossingEx exInfo = new CrossingEx();
			   BeanUtil.copyProperties(exInfo, crInfo);
			   crssionExList.add(exInfo);
			   
			   List<LocalEquipment>  equmentList = crossionService.queryEquimentByCrossionId(crInfo.getUuid());
			  
			   if(Tools.isEmpty(equmentList))
			   {
				   continue;
			   }
			   
			   List<LocalEquipmentEx> equmentExList = new ArrayList<LocalEquipmentEx>();
			   exInfo.setEquimentList(equmentExList);
			   
			   for(LocalEquipment equimentInfo : equmentList)
			   {
				   LocalEquipmentEx equimentEx = new LocalEquipmentEx();
				   BeanUtil.copyProperties(equimentEx, equimentInfo);
				   equmentExList.add(equimentEx);
				   
				   EquipmentToMedieExample mediaEx = new EquipmentToMedieExample();
				   mediaEx.or().andEquimentUuidEqualTo(equimentInfo.getEquipmentUuid());
				   List<EquipmentToMedie>  mediaList = equipmentToMedieMapper.selectByCondition(mediaEx);
				   
				   equimentEx.setMediaInfo(mediaList.isEmpty()?null:mediaList.get(0));
			   }
			   
			   //设备流媒体信息
		   }//end for 
		   
		   localStationEx.setListCrossing(crssionExList);
		}
		
		return localStationEx;
	}

	
	
	
	
	@Override
	public BaseMapper<LocalStationExample, LocalStation, String> getMapper()
	{
		return localStationMapper;
	}

	@Override
	public LocalStation findByStationName(String stationName)
	{
		// TODO Auto-generated method stub
		return localStationMapper.findByStationName(stationName);
	}

	@Override
	public void deleteStation(List<String> uuid)
	{
		// TODO Auto-generated method stub
		try
		{
			for (String id : uuid)
			{
				localStationMapper.deleteByPrimaryKey(id);
			}
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage(), e);
		}
	}

	
}
